<template>
  <div class="p-2 modal_main">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" size="small" label-width="80">
            <el-form-item label="统计名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入" clearable />
            </el-form-item>
            <el-form-item label="机型" prop="aircraft">
              <el-select v-model="queryParams.aircraft" clearable>
                <el-option v-for="(item, index) in pageAircraftTypes" :key="'type' + index" :label="item.label" :value="item.label" />
              </el-select>
            </el-form-item>
            <el-form-item style="padding-left: 80px">
              <el-button size="small" type="primary" @click="handleQuery">查询</el-button>
              <el-button size="small" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['result:statistic:add']" size="small" type="primary" plain icon="Plus" @click="handleAdd">新增统计</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-if="showTable" v-loading="loading" :data="tableDatas">
        <el-table-column label="序号" width="50" type="index" align="center">
          <template #default="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="统计名称" align="center" prop="name"></el-table-column>
        <el-table-column label="机型" align="center" prop="aircraft"></el-table-column>
        <el-table-column label="状态" align="center" prop="staus" width="200">
          <template #default="scope">
            {{ statusObj[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="createBy" />
        <el-table-column label="创建日期" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="400">
          <template #default="scope">
            <el-button v-hasPermi="['result:statistic:detail']" size="small" link type="primary" @click="handleDetail(scope.row)">详情</el-button>
            <el-button
              v-hasPermi="['result:statistic:edit']"
              size="small"
              link
              type="primary"
              :disabled="scope.row.status !== 'INIT'"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button v-hasPermi="['result:statistic:setting']" size="small" link type="primary" @click="handleSetting(scope.row)"
              >脚本及参数设置</el-button
            >
            <el-button v-hasPermi="['result:statistic:view']" size="small" link type="primary" @click="handleView(scope.row)">查看结果</el-button>
            <el-button v-hasPermi="['result:statistic:envelope']" size="small" link type="primary" @click="handleEnvelope(scope.row)"
              >查看包线</el-button
            >
            <el-button v-hasPermi="['result:statistic:delete']" size="small" link type="primary" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagenation">
        <pagination
          v-show="total > 0"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          layout="prev, pager, next,jumper"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
    <StatisticDetailDialog ref="statisticDetailDialogRef" @on-refresh="getList" />
    <ScriptParameterSettingDialog ref="scriptParameterSettingDialogRef" />
  </div>
</template>

<script setup name="LoadTaskManagement" lang="ts">
import { getVersionList } from '@/api/loadProcessingAlgorithm/loadVersionManagement/index';
import { aircraftTypes } from '@/api/loadProcessingAlgorithm/index';
import { getTaskWorkspaceList, deleteTaskWorkspace } from '@/api/loadProcessingAlgorithm/experimentResultStatistic/index';
import { dataQuery, dataVO } from '@/api/loadProcessingAlgorithm/loadTaskManagement/type';
import { getToken } from '@/utils/auth';
import StatisticDetailDialog from './statisticDetailDialog.vue';
import ScriptParameterSettingDialog from './scriptParameterSettingDialog.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const router = useRouter();
const tableDatas = ref([]);
const showTable = ref(true);
const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const pageAircraftTypes = ref(aircraftTypes);
const dateRangeCreateTime = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const detailDialogShow = ref(false);
const dataId = ref('');
const versionId = ref('');
const statisticDetailDialogRef = ref();
const scriptParameterSettingDialogRef = ref();
const queryParams = reactive<dataQuery>({
  pageNum: 1,
  pageSize: 10,
  name: '',
  aircraft: ''
  // orderByColumn: 'CreateTime',
  // isAsc: 'desc'
});
const statusObj = reactive({
  'INIT': '初始化',
  'CREATING': '创建中',
  'CREATED': '创建完毕'
});
const handleDialogClose = () => {
  detailDialogShow.value = false;
};
const handleView = (data) => {
  router.push({ path: '/viewResult', query: { id: data.id, loadType: data.loadType } });
};
const handleEdit = (data) => {
  statisticDetailDialogRef.value.openDialog('edit', data.id);
};
const handleDetail = (data) => {
  statisticDetailDialogRef.value.openDialog('view', data.id);
};
const handleAdd = () => {
  statisticDetailDialogRef.value.openDialog('add');
};
const handleEnvelope = (data) => {};
const handleSetting = (data) => {
  scriptParameterSettingDialogRef.value.openDialog(data.id);
};
/** 删除按钮操作 */
const handleDelete = async (row?) => {
  await proxy?.$modal.confirm('是否确认删除该数据?');
  loading.value = true;
  await deleteTaskWorkspace(row.id).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};
/** 查询模型列表 */
const getList = async () => {
  loading.value = true;
  if (dateRangeCreateTime.value.length) {
    queryParams.beginCreateTime = dateRangeCreateTime.value[0];
    queryParams.endCreateTime = dateRangeCreateTime.value[1];
  } else {
    queryParams.beginCreateTime = '';
    queryParams.endCreateTime = '';
  }
  const response = await getTaskWorkspaceList(queryParams);
  tableDatas.value = response.rows;
  total.value = response.total;
  loading.value = false;
  showTable.value = true;
};
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  showTable.value = false;
  dateRangeCreateTime.value = [];
  queryFormRef.value?.resetFields();
  handleQuery();
}
onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.modal_main {
  height: calc(100vh - 84px);
  overflow: auto;
}
.pagenation {
  display: flex;
  justify-content: end;
  margin-top: 10px;
}
.dialog-footer {
  text-align: center;
}
:deep(.el-upload) {
  display: block;
}
</style>
